package com.mmall.common;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
// 空的属性不会转换为json
public class ResponseService<T> {

    private int status;
    private String msg;
    private T data;

    private ResponseService(int status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }
    private ResponseService(int status,  T data) {
        this.status = status;
        this.data = data;
    }
    private ResponseService(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    // 通用响应
    public static ResponseService creteResponseSuccess(){
        return ResponseService.creteResponseSuccessByCode(ResultCode.SUCCESS);
    }
    public static <T> ResponseService creteResponseSuccess(String msg, T data ){
        return new ResponseService(ResultCode.SUCCESS.getStatus(),msg,data);
    }
    public static ResponseService creteResponseSuccessMsg(String msg){
        return new ResponseService(ResultCode.SUCCESS.getStatus(),msg);
    }
    public static ResponseService creteResponseSuccessByCode(ResultCode resultCode){
        return new ResponseService(resultCode.getStatus(),resultCode.getMsg());
    }
    public static <T> ResponseService creteResponseSuccessByData(T data){
        return new ResponseService(ResultCode.SUCCESS.getStatus(),ResultCode.SUCCESS.getMsg(),data);
    }
    public static <T> ResponseService  creteResponseSuccessData(int status , T data){
        return new ResponseService(status,data);
    }
    public static <T> ResponseService creteResponseSuccess(int status , String msg , T data){
        return new ResponseService(status,msg,data);
    }

//    失败响应
    public static ResponseService creteResponseFail(){
        return creteResponseFailByCode(ResultCode.OTHER_ERROR);
    }
    public static ResponseService creteResponseFailMsg(String msg){
        return new ResponseService(ResultCode.OTHER_ERROR.getStatus(),msg);
    }
    public static ResponseService creteResponseFailByCode(ResultCode resultCode){
        return new ResponseService(resultCode.getStatus(),resultCode.getMsg());
    }
    public static <T> ResponseService creteResponseFailByData(T data){
        return new ResponseService(ResultCode.OTHER_ERROR.getStatus(),ResultCode.OTHER_ERROR.getMsg(),data);
    }
    public static <T> ResponseService creteResponseFail(int status , T data){
        return new ResponseService(status,data);
    }
    public static <T> ResponseService creteResponseFail(int status , String msg , T data){
        return new ResponseService(status,msg,data);
    }
















    @JsonIgnore
    //  json 格式化时候 忽略该字段
    public boolean isSuccess(){
        return this.status == ResultCode.SUCCESS.getStatus();
    }


    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
